home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / accounts / acct-1.3 / acct-1 / acct-1.3.73 / utils / accttrim.c < prev    next >
C/C++ Source or Header  |  1995-07-09  |  2KB  |  113 lines

  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <time.h>
  4. #include <fcntl.h>
  5. #include <linux/acct.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <unistd.h>
  10.  
  11. static char *Version = "$Id: accttrim.c,v 1.2 1995/07/09 21:30:00 jiivee Exp jiivee $";
  12.  
  13. void usage()
  14. {
  15.    fprintf(stderr, "Usage: accttrim [-n number] acctfile\n");
  16.    exit(1);
  17. }
  18.  
  19. int main(int argc, char **argv)
  20. {
  21.    int ch, nr, bytes, cnt = 0;
  22.    int fdin, fdout;
  23.    struct acct ac;
  24.    struct stat st;
  25.    char buf[BUFSIZ];
  26.    char tmpfile[256];
  27.    extern int optind;
  28.    extern char *optarg;
  29.  
  30.    while ((ch = getopt(argc, argv, "n:")) != EOF) {
  31.       switch (ch)
  32.       {
  33.          case 'n':
  34.             cnt = atoi(optarg);
  35.             break;
  36.          default:
  37.             usage();
  38.       }
  39.    }
  40.    argc -= optind;
  41.    argv += optind;
  42.  
  43.    if (!argc)
  44.       usage();
  45.  
  46.    /* Turn accounting off for now */
  47.    acct(NULL);
  48.  
  49.    if ((fdin = open(*argv, O_RDWR)) < 0) {
  50.       perror("open");
  51.       exit(1);
  52.    }
  53.  
  54.    if (fstat(fdin, &st)) {
  55.       perror("Cannot stat accountingfile");
  56.       exit(1);
  57.    }
  58.  
  59.    if (st.st_size < (cnt * sizeof(struct acct))) {
  60.       fprintf(stderr, "Not that many entries in accounting file\n");
  61.       exit(1);
  62.    }
  63.  
  64.    sprintf(tmpfile, "%s.lck", *argv);
  65.    if ((fdout = creat(tmpfile, 0600)) < 0) {
  66.       perror("creat");
  67.       exit(1);
  68.    }
  69.  
  70.    /*
  71.     * Dump the time we stripped it and write that as the first entry
  72.     * in the accountingfile.
  73.     */
  74.    memset(&ac, 0, sizeof(struct acct));
  75.    strcpy(ac.ac_comm, "STRIPPED");
  76.    time(&ac.ac_etime);
  77.    write(fdout, &ac, sizeof(struct acct));
  78.  
  79.    bytes = (cnt * sizeof(struct acct));
  80.    if (lseek(fdin, st.st_size - (sizeof(struct acct) * cnt), SEEK_SET) == -1) {
  81.       perror("lseek");
  82.       exit(1);
  83.    }
  84.  
  85.    /*
  86.     * Try to read large chunks if possible, speeds up thing a bit.
  87.     */
  88.    while (bytes > 0) {
  89.       if (bytes > BUFSIZ) {
  90.          nr = read(fdin, buf, BUFSIZ);
  91.          write(fdout, buf, nr);
  92.          bytes -= BUFSIZ;
  93.       } else {
  94.          nr = read(fdin, buf, bytes);
  95.          write(fdout, buf, nr);
  96.          bytes = 0;
  97.       }
  98.    }
  99.    close(fdout);
  100.    close(fdin);
  101.  
  102.    /*
  103.     * Ok relink the tempfile to the original accountingfile.
  104.     */
  105.    unlink(*argv);
  106.    rename(tmpfile, *argv);
  107.  
  108.    /* Ok turn accounting on again */
  109.    acct(*argv);
  110.  
  111.    return (0);
  112. }
  113.